<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\ResController;
use Validator;


/**
* 海报控制器
*/
class PosterController extends Controller
{
    /**
     * 显示现有海报列表
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {
        //查出所有海报信息
        $data = DB::table('poster') -> get();

        //显示海报列表
        return view('admin/poster/index',['data' => $data]);
    }

    /**
     * 添加新海报
     *
     * @return \Illuminate\Http\Response
     */
    public function getAdd()
    {
        //显示添加模板
        return view('admin/poster/add');
    }

    /**
     * 处理用户加入的海报
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postInsert(Request $request)
    {
        //接收数据
        $data = $request -> except('_token');

        //设置过滤规则
        $rules = [
            'image' => 'required',
            'url' => 'url|required',
        ];
        
        //设置错误消息
        $messages = [
                 'image.required' => '海报名必填',
                'url.required'  => '海报指向地址必填',
                'url.url'  => '地址不合法',
        ];

        //验证数据
        $validator = Validator::make($data,$rules,$messages);
       
        if ($validator->fails()) {
            return back()
                        ->withErrors($validator)
                        ->withInput();
        }

       //检测图片是否可用
        if (!$request->hasFile('pic')) {
            return back()
                        ->withErrors('没有图片上传')
                        ->withInput();
        }

        //检验上传的文件是否有效.
        $file = \Input::file('pic');
        if($file -> isValid()){
            
            $clientName = $file -> getClientOriginalName();//客户端文件名称..
            $clientType = $file -> getClientOriginalExtension();
            $arrType = ['jpg','png','git','jpeg'];
            if (!in_array($clientType, $arrType)) {
                return back();
            }
        }
       
        //保存图片并获得路径 
        $filename = mt_rand(1000,9999).time().'.'.$file -> getClientOriginalExtension();
        $res = $file -> move('public/image/poster/',$filename);
        if (!$res) {
            return back();
        }
        
        //获得所需插入数据库数据
        $path = '/public/image/poster/'.$filename;
        $data = $request -> only('image','url');
        $data['path'] = $path;
       
        //插入数据库
        $res = DB::table('poster') -> insert($data);

        //返回结果
        return self::res($res,'添加海报','/admin/poster/index','/admin/poster/add');
    }
    /**
     * 删除一张海报.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getDelete($id)
    {
        //删除一条数据
       $res = DB::table('poster') -> where('id',$id) -> delete();
        
        //返回结果
       return self::res($res,'删除','/admin/poster/index','/admin/poster/index');
    }


    /**
     * 结果处理
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    static public function res($res,$message,$url_su,$url_er,$id = ''){
        if ($res) {
         
           //跳转到员工列表并显示成功信息
           return redirect($url_su)
                        ->with(['message'=>$message.'成功','type'=>1]);
        }else{

           //跳转到添加页面
           return redirect($url_er.$id)
                        ->with(['message'=>$message.'失败','type'=>2]);
        }
    }
}
